﻿package base.loader {
	import flash.display.Sprite;
	import base.tf.TextFieldExt;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	import com.greensock.TweenNano;
	import com.greensock.easing.Linear;
	
	public class LoaderShow	extends Sprite implements ILoaderShow {
		
		public var objInit: Object;
		private var tFormatLoaderShow: TextFormat 
		private var tfLoaderShow: TextFieldExt;
		private var shapeLoaderShow: Sprite;
		private var shapeFrameLoaderShow: Sprite;
		private var arrIndObjContentFromQueue: Array;
		private var numContentToLoad: uint;
		private var _numContentLoaded: uint;
		private var _countContentToLoad: uint;
		
		public function LoaderShow(objInit: Object = null) {
			if (objInit == null) objInit = { };
			this.objInit = objInit;
			this.createTextFieldLoaderShow();
			this.createShapeAndShapeFrameLoaderShow();
			this.numContentToLoad = 0;
			this.numContentLoaded = 0;
			this.countContentToLoad = 0;
			this.arrIndObjContentFromQueue = new Array;
		}
		
		private function createTextFormatLoaderShow(): TextFormat {
			if (this.objInit.fontName == undefined) this.objInit.fontName = "Arial";
			if (this.objInit.fontSize == undefined) this.objInit.fontSize = 12;
			if (this.objInit.fontColor == undefined) this.objInit.fontColor = 0x000000;
			if (this.objInit.fontBold == undefined) this.objInit.fontBold = true;
			if (this.objInit.fontItalic == undefined) this.objInit.fontItalic = false;
			return new TextFormat(this.objInit.fontName, this.objInit.fontSize, this.objInit.fontColor, this.objInit.fontBold, this.objInit.fontItalic, false, "", "", TextFormatAlign.LEFT, 0, 0, 0, 0);
		}
		
		private function createTextFieldLoaderShow(): void {
			this.tFormatLoaderShow = this.createTextFormatLoaderShow()
			this.tfLoaderShow = new TextFieldExt();
			this.tfLoaderShow.selectable = false;
			this.tfLoaderShow.multiline = false;
			this.tfLoaderShow.wordWrap = false;
			this.tfLoaderShow.autoSize = TextFieldAutoSize.LEFT;
			this.tfLoaderShow.text = "";
			this.tfLoaderShow.embedFonts = true;
			this.tfLoaderShow.setTextFormat(this.tFormatLoaderShow);
			this.tfLoaderShow.x = 0;
			this.tfLoaderShow.y = 0;
			this.addChild(this.tfLoaderShow);
		}

		private function createShapeAndShapeFrameLoaderShow(): void {
			if (this.objInit.shapeColor == undefined) this.objInit.shapeColor = this.objInit.fontColor;
			if (this.objInit.shapeHeight == undefined) this.objInit.shapeHeight = 10;
			if (this.objInit.shapeMaxWidth == undefined) this.objInit.shapeMaxWidth = 300;
			if (this.objInit.distanceTextFromShape == undefined) this.objInit.distanceTextFromShape = 15;
			if (this.objInit.shapeFrameColor == undefined) this.objInit.shapeFrameColor = 0xFF0000;
			if (this.objInit.marginXShapeFromShapeFrame == undefined) this.objInit.marginXShapeFromShapeFrame = 3;
			if (this.objInit.marginYShapeFromShapeFrame == undefined) this.objInit.marginYShapeFromShapeFrame = this.objInit.marginXShapeFromShapeFrame;
			this.shapeFrameLoaderShow = this.drawSpriteLoaderShow(1);
			this.shapeLoaderShow = this.drawSpriteLoaderShow(0);
		}
		
		private function drawSpriteLoaderShow(isShapeFrame: uint): Sprite {
			var marginX: Number = [this.objInit.marginXShapeFromShapeFrame, 0][isShapeFrame];
			var marginY: Number = [this.objInit.marginYShapeFromShapeFrame, 0][isShapeFrame];
			var spriteLoaderColor: Number = [this.objInit.shapeColor, this.objInit.shapeFrameColor][isShapeFrame];
			var spriteLoaderShow: Sprite = new Sprite();
			spriteLoaderShow.graphics.beginFill(spriteLoaderColor, 1);
			spriteLoaderShow.graphics.moveTo(0, 0);
			spriteLoaderShow.graphics.lineTo(1, 0);
			spriteLoaderShow.graphics.lineTo(1, this.objInit.shapeHeight - 2 * marginY);
			spriteLoaderShow.graphics.lineTo(0, this.objInit.shapeHeight - 2 * marginY);
			spriteLoaderShow.graphics.lineTo(0, 0);
			spriteLoaderShow.graphics.endFill();
			spriteLoaderShow.x = this.tfLoaderShow.x + marginX;
			spriteLoaderShow.y = this.tfLoaderShow.y + this.tfLoaderShow.height + this.objInit.distanceTextFromShape + marginY;
			this.addChild(spriteLoaderShow);
			return spriteLoaderShow;
		}
		
		internal function get numContentLoaded(): uint {
			return this._numContentLoaded;
		}
		
		internal function set numContentLoaded(value: uint): void {
			this._numContentLoaded = value;
		}
		
		internal function get countContentToLoad(): uint {
			return this._countContentToLoad;
		}
		
		internal function set countContentToLoad(value: uint): void {
			this._countContentToLoad = value;
		}
		
		internal function showToLoad(objLoadContent: Object): void {
			var strTypeContent: String = "";
			switch (objLoadContent.type) {
				case LoadContentQueue.IMAGE: strTypeContent = "obrazu"; break;
				case LoadContentQueue.SWF: strTypeContent = "swfa"; break;
				case LoadContentQueue.SOUND: strTypeContent = "dźwięku"; break;
				case LoadContentQueue.SERVER_DATA: strTypeContent = "danych"; break;
			}
			this.numContentToLoad++;
			this.tfLoaderShow.text = "Ładowanie " + strTypeContent + ": " + objLoadContent.url + " (" + String(this.numContentToLoad) + "/" + this.countContentToLoad + ")";
			this.tfLoaderShow.setTextFormat(this.tFormatLoaderShow);
			TweenNano.to(this.shapeLoaderShow, 10, {width: (this.numContentToLoad / this.countContentToLoad) * (this.objInit.shapeMaxWidth - 2 * this.objInit.marginXShapeFromShapeFrame), useFrames: true, ease: Linear.easeNone});
			TweenNano.to(this.shapeFrameLoaderShow, 10, {width: (this.numContentToLoad / this.countContentToLoad) * this.objInit.shapeMaxWidth, useFrames: true, ease: Linear.easeNone});
		}
		
		internal function showAllLoaded(): void {
			if (this.numContentLoaded == this.countContentToLoad) {
				this.tfLoaderShow.text = "Załadowano wszystko!";
			} else {
				this.tfLoaderShow.text = "Nie wszystko zostało załadowane!";
			}
		}
		
		
	}
	
}